package test.enba.mq.producer.controller.rabbitmq;

import com.enba.boot.mq.MqHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import test.enba.mq.producer.controller.rabbitmq.direct.DirectExchangeProducer;
import test.enba.mq.producer.controller.rabbitmq.fanout.FanoutExchangeProducer;
import test.enba.mq.producer.controller.rabbitmq.header.HeadersExchangeProducer;
import test.enba.mq.producer.controller.rabbitmq.topic.TopicExchangeProducer;

@RestController
@RequestMapping("/rabbitmq")
public class RabbitmqController {

  @Autowired private MqHelper mqHelper;

  @Autowired private DirectExchangeProducer directExchangeProducer;
  @Autowired private TopicExchangeProducer topicExchangeProducer;
  @Autowired private FanoutExchangeProducer fanoutExchangeProducer;
  @Autowired private HeadersExchangeProducer headersExchangeProducer;

  /**
   * 直接交换器发送消息队列
   *
   * @param message
   * @return
   */
  @PostMapping("/direct/send-message")
  public ResponseEntity<String> sendMessageByDirect(@RequestParam String message) {
    directExchangeProducer.sendMessage(message);
    return ResponseEntity.ok("directExchangeProducer发送消息成功");
  }

  /**
   * 广播交换器发送消息队列
   *
   * @param message
   * @return
   */
  @PostMapping("/fanout/send-message")
  public ResponseEntity<String> sendMessageByFanout(@RequestParam String message) {
    fanoutExchangeProducer.sendMessage(message);
    return ResponseEntity.ok("fanoutExchangeProducer发送消息成功");
  }

  /**
   * 主题交换器发送消息队列
   *
   * @param routingKey
   * @param message
   * @return
   */
  @PostMapping("/topic/send-message")
  public ResponseEntity<String> sendMessageByTopic(
      @RequestParam String routingKey, @RequestParam String message) {
    topicExchangeProducer.sendMessage(routingKey, message);
    return ResponseEntity.ok("topicExchangeProducer发送消息成功");
  }

  /**
   * 头交换器发送消息队列
   *
   * @param message
   * @return
   */
  @PostMapping("/headers/send-message")
  public ResponseEntity<String> sendMessageByHeaders(@RequestParam String message) {
    headersExchangeProducer.sendMessage(message);
    return ResponseEntity.ok("headersExchangeProducer发送消息成功");
  }

  /**
   * queue 发送消息
   *
   * @param message
   * @return
   */
  @PostMapping("/queue/send-message")
  public ResponseEntity<String> sendMessageByQueue(@RequestParam String message) {
    return mqHelper.sendMessageByQueue("queue", message, () -> ResponseEntity.ok("发送队列消息成功"));
  }

  /**
   * topic 发送消息
   *
   * @param message
   * @return
   */
  @PostMapping("/topic2/send-message")
  public ResponseEntity<String> sendMessageByTopic(@RequestParam String message) {
    return mqHelper.sendMessageByTopic("fanout-exchange", message, () -> ResponseEntity.ok("发送队列消息成功"));
  }
}
